<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends CI_Controller {
	//构造函数
	public function __construct(){
		parent::__construct();
		$this->load->model('Unit_model','unit');
		$this->load->model('Good_model','good');
		$this->load->model('Hire_model','hire');
	}
	
	/*
	 * 显示所有器材
	 */
	public function home(){
		$jsonp = $_GET["callback"];
		$id = $_GET["id"];
		$goods = $this->good->resultAll(array('unit' => $id));
		$unit_name = $this->unit->row(array('classid' => $id));
		if($unit_name){
			$data['status'] = 1;
			$data['goods'] = $goods;
			$data['unit_name'] = $unit_name;
			$data['is_school'] = $unit_name['is_school'];
			$data['map'] = $unit_name['map'];
		}else{
			$data['status'] = 0;
		}
		echo $jsonp."(".json_encode($data).")";
		
	}
	
	public function pay(){
		$jsonp = $_GET["callback"];
		$id = $_GET["id"];
		$unit = $this->unit->row(array('classid' => $id));
		if($unit){
			$data['status'] = 1;
			$data['img']=$unit['pay'];
		}else{
			$data['status'] = 0;
		}
		echo $jsonp."(".json_encode($data).")";
	}


	/*
	 * 内容详情页
	 */
	public function content(){
		$jsonp = $_GET["callback"];
		$id = $_GET["id"];
		$content = $this->good->row(array('id' => $id));		
		if($content){
			$data['status'] = 1;
			$data['content'] = $content;
		}else{
			$data['status'] = 0;
		}
		echo $jsonp."(".json_encode($data).")";
	}
	
	/*
	 * 租借
	 */
	public function hire(){
		$jsonp = $_GET["callback"];
		$id = $_GET["id"];
		$cost = $_GET["cost"];
		$time = $_GET["time"];
		$good = $this->good->row(array('id' => $id));
		$unit = $this->unit->row(array('classid' => $good["unit"]));
		$user = $this->session->userdata('user_name');
		$content = $this->hire->insert(array('name' => $good["name"],'cost' => $cost,'time' => $time,'unit' =>$unit["area"],'user' => $user,'create_time' => time()));		
		if($content){
			$data['status'] = 1;
		}else{
			$data['status'] = 0;
		}
		echo $jsonp."(".json_encode($data).")";
	}
	
	/*
	 * 搜索器材
	 * */
	public function search_good(){
		$jsonp = $_GET["callback"];
		$search = $_GET["search"];
		$id = $_GET["id"];
		$where = 'unit = '.$id.' and (name like "%'.$search.'%")';
		$re = $this->good->resultAll($where);
		if($re){
			$data['status'] = 1;
			$data['res'] = $re;
		}else{
			$data['status'] = 0;
		}
		echo $jsonp."(".json_encode($data).")";
	}
	
	/*
	 * 首页所有地区
	 * */
	public function area(){
		$jsonp = $_GET["callback"];
		$area = $this->unit->resultAll(array('level' => 1),'convert(area using gbk) ASC');
		$data['status'] = 1;
		$data['area'] = $area;
		echo $jsonp."(".json_encode($data).")";
	}
	
	/*
	 * 首页显示地区所有支持的单位
	 * */
	public function unit(){
		$jsonp = $_GET["callback"];
		$id = $_GET["id"];
		$area = $this->unit->row(array('classid' => $id));
		$area_name = $area["area"];
		$unit = $this->unit->resultAll(array('parentid' => $id),'convert(area using gbk) ASC');
		$data['status'] = 1;
		$data['unit'] = $unit;
		$data['area_name'] = $area_name;
		echo $jsonp."(".json_encode($data).")";
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */